package com.googlecode.player.services
{
	import com.googlecode.data.ProjectConstants;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class AnimationTimer extends EventDispatcher
	{
		private static var _instance:AnimationTimer;
		
		private var _timer:Timer = new Timer(int(1000 / ProjectConstants.ANIMATION_FRAME_RATE));
		
		public static const EVENT:String = "animationTick";
		
		public function AnimationTimer(target:IEventDispatcher=null)
		{
			super(target);
			
			_timer.addEventListener(TimerEvent.TIMER, onTick);
			_timer.start();
			
		}
		
		protected function onTick(evt:TimerEvent):void
		{
			dispatchEvent(new Event(EVENT));
		}
		
		/**
		 * Singleton. 
		 * @return 
		 * 
		 */
		public static function getInstance():AnimationTimer
		{
			
			if(_instance == null)
			{
				_instance = new AnimationTimer();
			}
			
			return _instance as AnimationTimer;
		}
		
	}
}